package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;

import java.util.List;

public class TvSetMenuPlayGridViewAdapter extends ArrayAdapter<PlayMenuTvVo> {

    private List<PlayMenuTvVo> itmes;
    private Context mcontext;

    public TvSetMenuPlayGridViewAdapter(@NonNull Context context, List<PlayMenuTvVo> itmes) {
        super(context, 0);
        this.itmes = itmes;
        this.mcontext = context;
    }

    public void addAll(List<PlayMenuTvVo> itmes) {
        if (itmes == null || itmes.size() < 1) {
            return;
        }
        this.itmes.clear();
        this.itmes.addAll(itmes);
        notifyDataSetChanged();
    }

    public void addItem(PlayMenuTvVo news) {
        if (news != null || news.getPlayWebUrl().length() < 1) {
            this.itmes.add(news);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (this.itmes == null || this.itmes.size() < 1) {
            return 0;
        }
        return this.itmes.size();
    }

    @Nullable
    @Override
    public PlayMenuTvVo getItem(int position) {
        if (position < 0 || position > itmes.size()) {
            return null;
        }
        return this.itmes.get(position);
    }

    public List<PlayMenuTvVo> getItmes() {
        return itmes;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position > itmes.size()) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    private View getCreateView(ViewGroup parent) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
        view = layoutInflater.inflate(R.layout.item_tv_play_menu_set, parent, false);
        return view;
    }

    class ViewHolder {
        TextView title;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        PlayMenuTvVo news = itmes.get(position);
        if (convertView == null) {
            convertView = getCreateView(parent);
            ViewHolder holder = new ViewHolder();
            if (convertView != null) {
                holder.title = convertView.findViewById(R.id.title);
                convertView.setTag(holder);
            }
        }
        if (convertView != null) {
            final ViewHolder holder = (ViewHolder) convertView.getTag();
            if (holder.title != null) {
                holder.title.setText(news.getName());
            }
        }

        return convertView;
    }
}
